﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DATA
{
   public  class NewsTypeREP
    {
        MODELGENERALDataContext context = new MODELGENERALDataContext();


        public List<NewsTypeDTO> GetAllNewsTypes()
        {
            List<NewsTypeDTO> acc = new List<NewsTypeDTO>();

            var acounts = from a in context.NewsTypes
                          select a;
            foreach (var account in acounts)
            {
                NewsTypeDTO nsDTO = new NewsTypeDTO();
                nsDTO.Description = account.description;
                nsDTO.IsActived = account.isActived;
                nsDTO.TypeID = account.typeID;
                nsDTO.TypeName = account.typeName;
                acc.Add(nsDTO);
            }
            return acc;
        }

        public List<NewsTypeDTO> GetActiveNewsType()
        {
            List<NewsTypeDTO> acc = new List<NewsTypeDTO>();

            var acounts = from a in context.NewsTypes
                          where a.isActived == true
                          orderby a.typeName ascending
                          select a;
            foreach (var account in acounts)
            {
                NewsTypeDTO nsDTO = new NewsTypeDTO();
                nsDTO.Description = account.description;
                nsDTO.IsActived = account.isActived;
                nsDTO.TypeID = account.typeID;
                nsDTO.TypeName = account.typeName;
                acc.Add(nsDTO);
            }
            return acc;
        }

        public bool AddNewsType(NewsTypeDTO newAccount)
        {
            try
            {
                NewsType ns = new NewsType();
                ns.typeID = newAccount.TypeID;
                ns.typeName = newAccount.TypeName;
                ns.isActived = true;
                ns.description = newAccount.Description;
                context.NewsTypes.InsertOnSubmit(ns);
                context.SubmitChanges();
            }
            catch (Exception)
            { return false; }
            return true;
            
        }
        public void DeleteNewsType(int AccountID)
        {
            var acc = context.NewsTypes.Single(a => a.typeID == AccountID);
            context.NewsTypes.DeleteOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdateNewsType(NewsTypeDTO newsTypeDTO)
        {
            try
            {
                NewsType acc = context.NewsTypes.Single( nv => nv.typeID == newsTypeDTO.TypeID  );
                acc.typeName = newsTypeDTO.TypeName;
                acc.isActived = newsTypeDTO.IsActived;
                acc.description = newsTypeDTO.Description;
                context.SubmitChanges();
            }
            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
